<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * array('type'=>0,
       'yAxis'=>array('yy'=>array(0=>1,1=>''),'tnmj'=>array(0=>'0-50|50-100',1=>'4')),
       'vAxis'=>array('jzmj'=>array(0=>'sum',1=>'4'),'tnmj'=>array(0=>'sum,avg',1=>'4')),
       'ranking'=>array('topNum'=>10,'topColumn'=>array(0=>'JZMJ',1=>'SUM',2=>'desc'))
 )
 array('type'=>1,
       'xAxis'=>array('yy'=>array(0=>1,1=>''),'qy'=>array(0=>1,1=>''),'jzmj'=>array(0=>'0-50|50-100',1=>'4')),
       'yAxis'=>array('yy'=>array(0=>1,1=>''),'tnmj'=>array(0=>'0-50|50-100',1=>'4')),
       'vAxis'=>array('jzmj'=>array(0=>'sum',1=>'4'),'tnmj'=>array(0=>'sum,avg',1=>'4')),
       'count'=>array('isXSum'=>1,'isYSum'=>0)
 )
 */

namespace BI\Controller;

/**
 * Description of FeedBackController
 *
 * @author chenghui
 */
class ReturnResultController extends FetchDataController{
    
    /**
     * @根据参数，自动判断分析方法。此方法用于外部
     * @return type
     */
    public function getAnalyzeDataResults(){
        //1.获得数据源【包括表头、表体、表脚三个数组】
  
        switch ($this->type){
            case 1://排行榜基础数据展示
                $result = $this->getTopData();
                break; 
            case 2://同比及环比基础表展示
                $result = $this->getYear2MonthMainData();
                break;
            case 3://供应存量价分析
                $result = $this->getAnalyzeData();
                break;
            case 9://挖掘
                $result = $this->getDigData();
                break;
            case 0://交叉分析基础表展示
            default :
                $result = $this->getAnalyzeData();
                break;
        }        

        //7.删除临时数据表
        $this->doDropTempTable($this->tempTableArr);
        return $result;
    }
    
    /**
     * @获得排名数据结果
     * @return type
     */
    private function getTopData(){
        $data = $this->getToppicMainData();
        //2.表头输出
        $result['d']['h'] = $this->setHeaderOutFormat($data['head']);

        //3.表体输出
        $result['d']['r'] = $this->setRowsOutFormat($data['rows']);
        
        //4.表脚输出
        $result['d']['f'] = $this->setFootOutFormat($data['foot']);
        
        //5.数据挖掘
        $result['d']['c'] = $this->setDataConfigFormat();
        
        //6.图表输出
        $result['c'] = $this->setChartOutFormat();

        return $result;
    }

    
    /**
     * @获得交叉分析数据结果
     * @return type
     */
    private function getAnalyzeData(){
   
   
        $data = $this->getAnalyzeMainData();
        //2.表头输出
        $result['d']['h'] = $this->setHeaderOutFormat($data['head']);

        //3.表体输出
        $result['d']['r'] = $this->setRowsOutFormat($data['rows']);
        
        //4.表脚输出
        $result['d']['f'] = $this->setFootOutFormat($data['foot']);
        
        //5.数据挖掘
        $result['d']['c'] = $this->setDataConfigFormat();
        
        //6.图表输出
        $result['c'] = $this->setChartOutFormat();
 
        return $result;
    }
    
    
    /**
     * @返回挖掘数据【此格式用于数据挖掘】
     * @return type
     */
    private function getDigData(){
        //1.获得数据源【包括表头、表体、表脚三个数组】
        $data = $this->getToppicMainData();
       
        $tLine=array();
        $head=array();

        foreach ($data['head'] as $val){
            foreach($val as $k=>$v){
                $tLine[] = $v['t'];
                $head[] = $v['f'];
            }            
        }
        
        $result[] = $tLine;

        foreach ($data['rows'] as $val){
            $rLine = array();
            foreach($head as $key){
                $rLine[] = $val[$key];
            }
            $result[] = $rLine;
        }
        return $result;
    }
    
    /*
     * *************************************************************************
     * 以上为输入最终结果
     * *************************************************************************
     */
    
    /**
     * @格式化表头输出格式
     * @param type $data
     * @return string
     */
    private function setHeaderOutFormat($data){
        $head['i']=$data;
        $head['f']=$this->getFixedColumn();//fixld简称，左边固定列名
        $head['r']=$this->getMergeHeadColumn();//需要纵向合并的列
        $head['a']=$this->getDigHead();//表头维度值，用于挖掘
        if($this->type==1){
            $head['g'] = $this->ranking['topColumn'][0].'_'.$this->ranking['topColumn'][1];//表头排序
        }
        
        return $head;
    }

    /**
     * @方法：设定左侧固定列代号
     * @参数：y维度数组
     * @返回：一维数组
     */
    private function getFixedColumn(){
        if($this->yNums > 0){//存在y维度
            if($this->type == 1){//排行榜
                $fixed[]='rank';
            }
            foreach ($this->yAxis as $key){
                $fixed[]=$key;
            }
        }else{//锁定纵向值
            $fixed[]='yAxis';
        }
        
        return $fixed;
    }
    
    /**
     * @方法：根据传入的参数，判断列中需要合并的表头列
     * @参数：y维度
     * @返回：数组
     */
    private function getMergeHeadColumn(){
        $rowspan=array();
        if($this->yNums > 0){//存在y维度            
            foreach($this->yAxis as $key){
                $rowspan[] = $key;
            }
        }else{//锁定纵向值
            $rowspan[]='yAxis';
        }
        if($this->count['isXSum'] == 1){
            $rowspan[]='gTotal';
        }
        return $rowspan;
    }
    
    /**
     * @方法：根据传入的参数，判断表头维度
     * @参数：x维度
     * @返回：数组
     */
    private function getDigHead(){
        $rowspan=array();
        if($this->xNums > 0){//存在x维度
            if($this->yNums > 0){
                foreach($this->xAxis as $key){
                    $rowspan[] = $key;
                }
                    
                if($this->vNums>1){//多个统计值
                    $rowspan[] = 'COUNT';
                }
            }else{
                foreach($this->xAxis as $key){
                    $rowspan[] = $key;
                }
            }
        }else{
            $rowspan[] = 'COUNT';
        }
        return $rowspan;
    }

    
    
    /**
     * @方法：解析表头数组，将结果解析成表格输出所需要格式
     * @参数：表头数组
     * @返回：表头数组
     */
    private function setRowsOutFormat($data){
        $rows['i']=$data;
        $rows['f'] = $this->getFixedColumn();//固定列
        $rows['h'] = $this->getColorColumn();//颜色不涉及到的列
        $rows['r'] = $this->getMergeColumn();//合并列
        $rows['a'] = $this->getDigRows();//用于数据挖掘的表示
        if($this->type==1){
            $rows['g'] = $this->ranking['topColumn'][0].'_'.$this->ranking['topColumn'][1];//表头排序
        }
        
        return $rows;
    }
    

    /**
     * @方法：根据传入的参数，判断行颜色不同的列
     * @参数：y维度
     * @返回：数组
     */
    private function getColorColumn(){
        if($this->yNums > 0){//存在y维度
            if($this->type == 1){//排行榜
                $color[]='rank';
            }
            foreach ($this->yAxis as $key){
                $color[]=$key;
            }
        }else{//锁定纵向值
            $color[]='yAxis';
        }
        return $color;
    }
    
    /**
     * @方法：根据传入的参数，判断列中需要合并的列
     * @参数：y维度
     * @返回：数组
     */
    private function getMergeColumn(){
        if($this->yNums > 1){//存在y维度
            $rowspan=array();
            $j=1;
            foreach($this->yAxis as $key){
                if($j < $this->yNums ){
                    $rowspan[] = $key;
                }
                ++$j;
            }
        }
        return $rowspan;
    }
    
    /**
     * @方法：根据传入的参数，判断横向维度值
     * @参数：y维度
     * @返回：数组
     */
    private function getDigRows(){
        $rowspan=array();
        if($this->yNums > 0){//存在y维度
            foreach($this->yAxis as $key){
                $rowspan[] = $key;
            }
        }else{
            $rowspan[] = 'COUNT';
        }
        return $rowspan;
    }
    
    
    /**
     * @方法：解析FOOT数组，将结果解析成表格输出所需要格式
     * @参数：表头数组
     * @返回：表头数组
     */
    private function setFootOutFormat($data){
        $foot['i']=$data;
        $foot['f']=$this->getFixedColumn();//fixld简称，左边固定列名
        $foot['h']=$this->getFixedColumn();//需要纵向合并的列
        
        return $foot;
    }
    
    /**
     * @方法：根据参数判断图表类型
     * @参数：
     * 柱状图	bar
        条形图	sybar
        折线图	line
        饼状图	pie
        堆积折线图	sline
        堆积柱状图	xBar
        堆积条形图	yBar
        百分比堆积柱状图	pxBar
        百分比堆积条形图	pyBar
        量价图	fixed
     * @返回：图表类型
     */
    private function setChartOutFormat(){
    
        //$cBase=array('bar','sybar','line','pie','sline','xBar','yBar','pxBar','pyBar','fixed');
        if($this->type == 1){
            $chart = $this->setTopChartAxis();
        }else{
            $chart = $this->setPovitChartAxis();
        }
        return $chart;
    }
    
    
    /**
     * @方法：解析交叉数据分析时的图表维度值
     */
    private function setTopChartAxis(){
        $c = $this->setChartType();
        $chart['t']=$c[1];
        
        $vAxis=array();
        if($this->vNums>0){
            foreach ($this->vAxis as $val){
                if($val['k']==$this->ranking['topColumn'][0]){
                    $uom=$val['u'];
                }
            }
            $vAxis[] = array($this->setColumnName($this->ranking['topColumn'][0]),$this->setCountName($this->ranking['topColumn'][1]),$this->setUomName($uom));
        }
        
        $xAxis[] = array($this->setColumnName($this->ranking['topColumn'][0]),$this->setCountName($this->ranking['topColumn'][1]),$this->setUomName($uom));

        
        $yAxis=array();
        if($this->yNums>0){
            foreach ($this->yAxis as $key){
                $yAxis[] = $this->setColumnName($key);
            }
        }
        
        $chart['x']=$xAxis;
        $chart['y']=$yAxis;
        $chart['v']=$vAxis;
        
        return $chart;
    }
    
    /**
     * @方法：解析交叉数据分析时的图表维度值
     */
    private function setPovitChartAxis(){
        $c = $this->setChartType();
     
        $chart['t']=$c[1];
        
        
        $vAxis=array();
        if($this->vNums>0){
             
            if($chart['t'][0]=='fixed'){
                foreach ($this->vAxis as $val){
                    switch ($val['k']){
                        case 'JMDJ':
                        case 'TNDJ':
                        case 'TS':
                            
                        case 'CJLMJ':
                        case 'CJDJ':
                        case 'CJPRICE':
                        case 'GGLMJ':
                        case 'GGDJ':
                        case 'PRICE':
                            $vAxis[] = array($this->setColumnName($val['k']),$this->setCountName($val['c']),$this->setUomName($val['u']),'r');
                            break;
                        case 'JZMJ':
                        case 'TNMJ':
                        case 'CJJE':
                            
                        case 'DIMENSION':
                            
                        case 'AREA':
                            
                        case 'ZS':
                        default :
                            $vAxis[] = array($this->setColumnName($val['k']),$this->setCountName($val['c']),$this->setUomName($val['u']),'l');
                            break;
                    }                    
                }
               
            }else{
                foreach ($this->vAxis as $val){
                    $vAxis[] = array($this->setColumnName($val['k']),$this->setCountName($val['c']),$this->setUomName($val['u']),'l');
                }
            }
        }
        
        $xAxis=array();
        if($this->xNums>0){
            foreach ($this->xAxis as $key){
                $xAxis[] = $this->setColumnName($key);
            }
        }else{
            foreach ($this->vAxis as $val){
                $xAxis[] = $this->setColumnName($val['k']);
            }
        }
        
        $yAxis=array();
        if($this->yNums>0){
            foreach ($this->yAxis as $key){
                $yAxis[] = $this->setColumnName($key);
            }
        }else{
            foreach ($this->vAxis as $val){
                $yAxis[] = $this->setColumnName($val['k']);
            }
        }
        
        $chart['xN']=$this->xNums;
        $chart['yN']=$this->yNums;
        $chart['vN']=$this->vNums;
        
        $chart['x']=$xAxis;
        $chart['y']=$yAxis;
        $chart['v']=$vAxis;
        
        return $chart;
    }
    
    
    /**
     * @方法：判断图表样式
     * @参数：维度
     */
    private function setChartType(){
   
        if($this->type == 1){
            $chart=array('sybar');
            $type=1;
        }else{
            //echo 123;
            if($this->vNums>1){
                $i=0;
                foreach ($this->vAxis as $val){
                    switch ($val['k']){
                        case 'JMDJ':
                        case 'TNDJ':
                        case 'TS':
                            
                        case 'CJLMJ':
                        case 'CJDJ':
                        case 'CJPRICE':
                        case 'GGLMJ':
                        case 'GGDJ':
                        case 'PRICE':
                            ++$i;
                            break;
                        case 'JZMJ':
                        case 'TNMJ':
                        case 'CJJE':
                            
                        case 'DIMENSION':
                            
                        case 'AREA':
                            
                        case 'ZS':
                            
                        default :
                            ;
                            break;
                    }
                }
                if($i>0){
                    $chart=array('fixed');
                    $type=1;
                }else{
                    $chart=array('bar','line','xBar','yBar','pie');
                    $type=0;
                }            
            }else{
                $chart=array('bar','line','xBar','yBar','pie');
                $type=0;
            }
        }
        return array(0=>$type,1=>$chart);
    }
    
    
    /**
     * @方法：解析每个字段的中文名称
     * @参数：统计字段$key
     * @返回：返回统计字段
     */
    private function setColumnName($key){
        return $this->AxisConfig[$key]['FieldName'];
    }
    
    /**
     * @方法：解析每个统计方式的名称
     * @参数：统计字段$count
     * @返回：返回统计字段
     */
    private function setCountName($count){
        return $this->CountConfig[$count]['CountName'];
    }
    
    /**
     * @方法：解析每个计量单位的名称
     * @参数：统计字段$uom
     * @返回：返回统计字段
     */
    private function setUomName($uom){
        return $this->UomConfig[$uom]['UomName'];
    }
    
    /**
     * 
     * @方法：挖掘数据
     * @return type
     * i为1表示要挖，为0表示不需要挖；
     */
    private function setDataConfigFormat(){
        $i=0;
        $config=array();
        if($this->xNums>0){
            foreach ($this->xAxis as $key){
                if($this->AxisConfig[$key]['FieldType']==3){
                    ++$i;
                }
            }
        }
        
        if($this->yNums>0){
            foreach ($this->yAxis as $key){
                if($this->AxisConfig[$key]['FieldType']==3){
                    ++$i;
                }
            }
        }
        //$config['i']为1表示是否挖掘
        if($this->type == 3){
            $config['i']=0;
        }else{
            $config['i']=1;
        }
        
        //$config['l']表示是否允许色阶
        if($i>0){
            $config['l']=1;
        }else{
            $config['l']=0;
        }
        //$config['t']表示挖掘类型
        $config['t']=$this->setDigType();

        return $config;
    }
    
    /**
     * @设置挖掘类型
     * @return type
     */
    private function setDigType(){
        $i=0;
        foreach ($this->allAxis as $xis){
            if($xis=='LPMC'){
                ++$i;
            }
        }
        
        if($i>0){
            $type = 2;
        }else{
            $type = 1;
        }
        return $type;
    }
}
